package com.baichuan.zntx.handler;

import com.baichuan.zntx.service.IMessageHandlerService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.json.JsonObjectDecoder;
import lombok.RequiredArgsConstructor;

/**
 * @author mazhanliang
 */
@RequiredArgsConstructor
public class NettyServerHandler extends ChannelInitializer<Channel> {
    private final IMessageHandlerService messageHandlerService;
    @Override
    protected void initChannel(Channel channel) throws Exception {
//        channel.pipeline().addLast(
//            new JsonObjectDecoder(),
//            new LoraJsonDecoder(),
//            new LoraJsonEncoder(),
//            new LoraJsonHandler(messageHandlerService)
//        );
        // 数据分割符
        String delimiterStr = "\r";
        ByteBuf delimiter = Unpooled.copiedBuffer(delimiterStr.getBytes());
        ChannelPipeline pipeline = channel.pipeline();
        // 使用自定义处理拆包/沾包，并且每次查找的最大长度为1024字节
        pipeline.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
        // 将上一步解码后的数据转码为Message实例
        pipeline.addLast(new MessageDecodeHandler());
        // 对发送客户端的数据进行编码，并添加数据分隔符
        pipeline.addLast(new MessageEncodeHandler(delimiterStr));
        // 对数据进行最终处理
        pipeline.addLast(new ServerListenerHandler(messageHandlerService));
    }
}
